package it.sportingnola.soccernotes;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TeamEdit extends Activity {
    private DbHelper mDbHelper;
    private short squadra;
    private long cronaca;
    private String nomeSquadra;
    private int nrPlayers;
    private LinearLayout main;
    private final int nrLabelInd=0;
    private final int nrTextInd=1;
    private final int idTextInd=2;
    private final int nameTextInd=3;
    private final int maxNrLength=3;
    private final int maxNameLength=30;
    private final LayoutParams lpl=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
	LinearLayout.LayoutParams.WRAP_CONTENT, 1); // layout grandi
    private final LayoutParams mps=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
	Const.STD_HEIGHT, 1); // layout medi
    private final LayoutParams lps=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
	Const.STD_HEIGHT, 0); // layout piccoli

    private LinearLayout createLinearLayout() {
	LinearLayout ret=new LinearLayout(this);
	ret.setOrientation(android.widget.LinearLayout.HORIZONTAL);
	ret.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
	return ret;
    }

    private EditText createNameEdit() {
	EditText ret=new EditText(this);
	InputFilter[] filterArray=new InputFilter[1];
	filterArray[0]=new InputFilter.LengthFilter(maxNameLength);
	ret.setFilters(filterArray);
	ret.setLayoutParams(mps);
	ret.setMaxLines(1);
	ret.setPadding(5, 5, 5, 5);
	ret.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
	ret.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
	return ret;
    }

    private EditText createNrEdit() {
	EditText ret=new EditText(this);
	InputFilter[] filterArray=new InputFilter[1];
	filterArray[0]=new InputFilter.LengthFilter(maxNrLength);
	ret.setFilters(filterArray);
	ret.setLayoutParams(lps);
	ret.setMaxLines(1);
	ret.setPadding(5, 5, 5, 5);
	ret.setInputType(InputType.TYPE_CLASS_NUMBER);
	ret.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
	return ret;
    }

    private TextView createNrLabel() {
	TextView ret=new TextView(this);
	ret.setText(getText(R.string.nr_ot));
	ret.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
	ret.setLayoutParams(lps);
	return ret;
    }

    private void fillData() {
	Cursor tc=mDbHelper.cercaSquadra(squadra, cronaca);
	startManagingCursor(tc);
	if ( ! tc.moveToFirst()) { // se non ci sono ancora giocatori
	    tc=mDbHelper.riempiSquadra(squadra, cronaca, nomeSquadra, nrPlayers); // li riempo
	    tc.moveToFirst();
	}
	LinearLayout ll;
	for (int i=0; i < main.getChildCount(); i++) {
	    View cv=main.getChildAt(i);
	    if (cv instanceof LinearLayout) {
		ll=(LinearLayout) cv;
		EditText nrText=(EditText) ll.getChildAt(nrTextInd);
		nrText.setText(tc.getString(tc.getColumnIndex(Const.Players.KEY_NR)));
		EditText nomiText=(EditText) ll.getChildAt(nameTextInd);
		nomiText.setText(tc.getString(tc.getColumnIndex(Const.Players.KEY_NAME)));
		EditText idText=(EditText) ll.getChildAt(idTextInd);
		idText.setText(tc.getString(tc.getColumnIndex(Const.Players.KEY_ROWID)));
		tc.moveToNext();
	    }
	}
	tc.close();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// recupera i parametri passati tramite Intent
	Bundle extras=getIntent().getExtras();
	if (extras != null) {
	    cronaca=extras.getLong(Const.Players.KEY_CRONACA);
	    squadra=extras.getShort(Const.Players.KEY_TEAM);
	    nomeSquadra=extras.getString(Const.Players.NAME);
	    nrPlayers=extras.getInt(Const.Config.KEY_DEFAULT_PL_NUMBER);
	}
	setTitle(nomeSquadra);
	setContentView(R.layout.team_edit);
	prepareFields();
	mDbHelper=new DbHelper(this);
	mDbHelper.open();
	fillData();
    }

    @Override
    protected void onPause() {
	super.onPause();
	saveState();
    }

    @Override
    protected void onStop() {
	super.onPause();
	saveState();
    }

    /**
     * crea i campi nella View
     */
    private void prepareFields() {
	main=(LinearLayout) findViewById(R.id.team_edit);
	TextView fcTitle=new TextView(this);
	fcTitle.setText(getText(R.string.first_choice));
	main.addView(fcTitle);
	TextView bTitle=new TextView(this);
	bTitle.setText(getText(R.string.bench));
	for (int i=0; i < nrPlayers; i++) {
	    if (i == Const.NR_TITOLARI) {
		main.addView(bTitle);
	    }
	    LinearLayout plLl=createLinearLayout();
	    TextView nrl=createNrLabel();
	    plLl.addView(nrl, nrLabelInd);
	    EditText nrt=createNrEdit();
	    plLl.addView(nrt, nrTextInd);
	    EditText idt=new EditText(this);
	    idt.setVisibility(View.GONE);
	    plLl.addView(idt, idTextInd);
	    EditText namet=createNameEdit();
	    plLl.addView(namet, nameTextInd);
	    main.addView(plLl);
	}
	// crazione dei campi per il mister: il campo per editare il numero è invisibile
	LinearLayout mrLl=createLinearLayout();
	TextView nrl=createNrLabel();
	nrl.setText(R.string.coach);
	mrLl.addView(nrl, nrLabelInd);
	EditText nrt=createNrEdit();
	nrt.setVisibility(View.GONE);
	nrt.setEnabled(false);
	mrLl.addView(nrt, nrTextInd);
	EditText idt=new EditText(this);
	idt.setVisibility(View.GONE);
	mrLl.addView(idt, idTextInd);
	EditText namet=createNameEdit();
	mrLl.addView(namet, nameTextInd);
	main.addView(mrLl);
	// bottone di salvataggio
	Button bs=new Button(this);
	bs.setLayoutParams(lpl);
	bs.setHeight(Const.STD_HEIGHT);
	bs.setText(R.string.save_and_close);
	bs.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		TeamEdit.this.salva(v);
	    }
	});
	main.addView(bs);
    }

    public void salva(View view) {
	setResult(RESULT_OK);
	finish();
    }

    private void saveState() {
	int is_mr;
	int is_fc;
	LinearLayout ll;
	int np=0;
	for (int i=0; i < main.getChildCount(); i++) {
	    View cv=main.getChildAt(i);
	    if (cv instanceof LinearLayout) {
		ll=(LinearLayout) cv;
		EditText nrText=(EditText) ll.getChildAt(nrTextInd);
		EditText nomiText=(EditText) ll.getChildAt(nameTextInd);
		EditText idText=(EditText) ll.getChildAt(idTextInd);
		is_mr=getString(R.string.coach).equals(nrText.getText().toString()) ? 1 : 0;
		is_fc=(np < Const.NR_TITOLARI) ? 1 : 0;
		mDbHelper.updatePlayer(Long.parseLong(idText.getText().toString()), squadra, cronaca, nrText.getText()
		    .toString(), nomiText.getText().toString(), is_mr, is_fc);
		np++;
	    }
	}
    }
}
